package com.lovo.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lovo.bean.Service;
import com.lovo.bean.Staff;
import com.lovo.bean.User;
import com.lovo.dao.ServiceMapper;
import com.lovo.dao.UserMapper;
import com.lovo.service.IServiceService;
import com.lovo.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ServiceServiceImpl implements IServiceService {

//    查询所有并分页显示
    @Override
    public PageInfo<Service> getPage(int pageNo, int pageSize) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ServiceMapper mapper = sqlSession.getMapper(ServiceMapper.class);
        PageHelper.startPage(pageNo,pageSize);
        List<Service> list = mapper.getAll();
        PageInfo<Service> page = new PageInfo<Service>(list);
        SqlSessionUtil.commit ();

        return page;
    }

//    查询所有服务员
    @Override
    public List<Staff> getStaff() {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ServiceMapper mapper = sqlSession.getMapper(ServiceMapper.class);
        List<Staff> list = mapper.getStaff();
        SqlSessionUtil.commit ();
        return list;
    }

//    添加服务
    @Override
    public boolean addService(Service service) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ServiceMapper mapper = sqlSession.getMapper(ServiceMapper.class);
        int n = mapper.insert (service);
        SqlSessionUtil.commit ();
        return n > 0;
    }

//    删除服务
    @Override
    public boolean delService(int serviceId) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ServiceMapper mapper = sqlSession.getMapper(ServiceMapper.class);
        int n = mapper.deleteByPrimaryKey (serviceId);
        SqlSessionUtil.commit ();
        return n > 0;
    }

//    修改服务
    @Override
    public boolean updateService(Service service) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ServiceMapper mapper = sqlSession.getMapper(ServiceMapper.class);
        int n = mapper.updateByPrimaryKeySelective (service);
        SqlSessionUtil.commit ();
        return n > 0;
    }

//    条件查询
    @Override
    public PageInfo<Service> getPageByQuery(int pageNo, int pageSize, Service service) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ServiceMapper mapper = sqlSession.getMapper(ServiceMapper.class);
        PageHelper.startPage(pageNo,pageSize);
        List<Service> list = mapper.selectByQuery (service);
        PageInfo<Service> page = new PageInfo<Service>(list);
        SqlSessionUtil.commit ();

        return page;
    }
}
